<template>
  <el-button @click="goBack" class="goback">返回</el-button>
  <el-card style="min-width: 480px" class="main">
    <template #header>
      <div class="card-header">
        <div class="title">发布帖子</div>
      </div>
    </template>
    <el-form
    ref="ruleFormRef"
    style="max-width: 600px"
    :model="ruleForm"
    status-icon
    label-width="auto"
    class="demo-ruleForm"
  >
    <el-form-item label="标题" prop="pass" >
      <el-input v-model="postForm.title"/>
    </el-form-item>
    <el-form-item label="内容" prop="age" >
      <el-input v-model="postForm.content" type="textarea"/>
    </el-form-item>
    <el-form-item label="图片" prop="age" >
        <el-upload
    class="avatar-uploader"
    action="/api/upload"
    :show-file-list="false"
    :on-success="handleAvatarSuccess"
    :before-upload="beforeAvatarUpload"
    accept=".jpg,.jpeg,.png"
  >
    <img v-if="postForm.imageUrl" :src="postForm.imageUrl" class="avatar" />
    <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
  </el-upload>
    </el-form-item>
  </el-form>
    <template #footer>
      
      <el-button type="primary" @click="submitForm" class="fabu">
        发布
      </el-button>
    </template>
  </el-card>
</template>

<script setup lang="ts">
import {onMounted, ref} from 'vue'
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import { useRouter } from "vue-router";
import {addPostAPI} from '../../api/home'
import type { UploadProps } from 'element-plus'
import useCounter from '../../stores/pinia';
  import { parseJwt } from '../../stores/pinia'

const router=useRouter()
const counterStore=useCounter()

const userInfo=ref({})

const postForm=ref({
    authorId:null,
    avatar:"",
    name:"",
    imageUrl:"",
    content:"",
    title:""
})


const goBack=()=>{
  router.push('/home')
}
const handleAvatarSuccess: UploadProps['onSuccess'] = (
  response,
  uploadFile
) => {
  // console.log(response.data)
  postForm.value.imageUrl = response.data
  // console.log(postForm.value.imageUrl)
}

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  // console.log(rawFile)
  const isCorrectExtension = rawFile.name.endsWith('.jpg') || rawFile.name.endsWith('.png')||rawFile.name.endsWith('.jpeg');
  if (!isCorrectExtension) {
    ElMessage.error('只能上传jpg或png格式的图片')
    return false
    
  } else if (rawFile.size / 1024/512 > 1.5) {
    ElMessage.error('图片大小不能超过1MB!')
    return false
  }
  return true
}
const submitForm=async()=>{
  // console.log("发表的帖子",postForm.value)
  postForm.value.authorId=userInfo.value.id
  postForm.value.avatar=userInfo.value.avatar
  postForm.value.name=userInfo.value.username
    const result=await addPostAPI(postForm.value)
    // console.log(postForm.value)
    if(result.data.code==0){
      ElMessage({
    message: '帖子发布成功',
    type: 'success',
  })

  postForm.value.authorId=""
    postForm.value.content=""
    postForm.value.imageUrl=""
    postForm.value.title=""
    }
}
onMounted(()=>{
  const jwt=counterStore.jwt
  userInfo.value=parseJwt(jwt).claims
  // console.log(userInfo.value.id)
})
</script>
<style scoped>
.goback{
  position: fixed;
  margin-left: 5%;
  margin-top: 50px;
  font-size:24px;
  color: #474a59;
  padding: 0 6px;
  font-weight: 500;
  border-color: #dbd2d2ac;
  border-bottom: 3px solid #dbd2d2ac;
  background-color: #dbd2d2ac;
}
.main{
    width: 60%;
    /* height: 90%; */
    background-color: #fff;
    /* display: flex; */
    margin: auto;
    margin-top: 50px;
    /* padding-top: 50px; */
}
.title{
  width: 99px;
  font-size:24px;
  color: #474a59;
  padding: 0 6px;
  font-weight: 500;
  border-color: #dbd2d2ac;
  border-bottom: 3px solid #dbd2d2ac;
  background-color: #dbd2d2ac;
  border-radius: 3px;
}
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.avatar-uploader{
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}
.el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}
.avatar-uploader:hover {
  border-color: var(--el-color-primary);
}
.el-upload:hover {
  border-color: var(--el-color-primary);
}
.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
.fabu{
  display: block;
  margin: auto;
  width: 30%;
  font-size:24px;
  color: #474a59;
  padding: 0 6px;
  font-weight: 500;
  border-color: #dbd2d2ac;
  border-bottom: 3px solid #dbd2d2ac;
  background-color: #dbd2d2ac;
  /* border-radius: 3px; */
}
</style>